package com.easyjava.qingke.project;

import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.easyjava.qingke.project.mybatis.IDao.CollarSingleMLMapper;
import com.easyjava.qingke.project.mybatis.IDao.CollarSingleMapper;
import com.easyjava.qingke.project.mybatis.IDao.DocumentRecordMapper;
import com.easyjava.qingke.project.mybatis.IDao.LibraryRecordMLMapper;
import com.easyjava.qingke.project.mybatis.IDao.LibraryRecordMapper;
import com.easyjava.qingke.project.mybatis.IDao.MaterialMapper;
import com.easyjava.qingke.project.mybatis.IDao.ProjectMapper;
import com.easyjava.qingke.project.mybatis.IDao.PurchaseOrderMLMapper;
import com.easyjava.qingke.project.mybatis.IDao.PurchaseOrderMapper;
import com.easyjava.qingke.project.mybatis.IDao.PurchasePlanMLMapper;
import com.easyjava.qingke.project.mybatis.IDao.PurchasePlanningMapper;
import com.easyjava.qingke.project.mybatis.IDao.StorageMLMapper;
import com.easyjava.qingke.project.mybatis.IDao.StorageRecordMapper;
import com.easyjava.qingke.project.mybatis.IDao.UserMapper;
import com.easyjava.qingke.project.mybatis.pojo.CollarSingle;
import com.easyjava.qingke.project.mybatis.pojo.CollarSingleML;
import com.easyjava.qingke.project.mybatis.pojo.DocumentRecord;
import com.easyjava.qingke.project.mybatis.pojo.LibraryRecord;
import com.easyjava.qingke.project.mybatis.pojo.LibraryRecordML;
import com.easyjava.qingke.project.mybatis.pojo.Material;
import com.easyjava.qingke.project.mybatis.pojo.Project;
import com.easyjava.qingke.project.mybatis.pojo.PurchaseOrder;
import com.easyjava.qingke.project.mybatis.pojo.PurchaseOrderML;
import com.easyjava.qingke.project.mybatis.pojo.PurchasePlanML;
import com.easyjava.qingke.project.mybatis.pojo.PurchasePlanning;
import com.easyjava.qingke.project.mybatis.pojo.StorageML;
import com.easyjava.qingke.project.mybatis.pojo.StorageRecord;
import com.easyjava.qingke.project.mybatis.pojo.User;

public class ProjectDao {

	public ProjectDao() {
	}
	String resource = "mybatis_conf.xml";
	InputStream is = ProjectDao.class.getClassLoader().getResourceAsStream(resource);
	SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
	SqlSession session = sessionFactory.openSession();
	
	
	public void selectUser(){
		UserMapper um=session.getMapper(UserMapper.class);
		List<User> user=um.selectUser();
		for(User u :user){
			System.out.println(u);
		}
	}
	public void addUser(User user){
		UserMapper um=session.getMapper(UserMapper.class);
		um.insert(user);
		session.commit();	
	}
	public void changeUser(User user){
		UserMapper um=session.getMapper(UserMapper.class);
		um.updateByPrimaryKeySelective(user);
		session.commit();
	}
	public void addProject(Project project){
		ProjectMapper pm=session.getMapper(ProjectMapper.class);
		pm.insert(project);
		session.commit();
	}
	public void changeProject(Project project){
		ProjectMapper pm=session.getMapper(ProjectMapper.class);
		pm.updateByPrimaryKeySelective(project);
		session.commit();
	}
	public void selectProject(){
		ProjectMapper pm=session.getMapper(ProjectMapper.class);
		List<Project> projects=pm.selectProject();
		for(Project project:projects){
			System.out.println(project);
		}
	}
	public void selectMaterial(){
		MaterialMapper mm=session.getMapper(MaterialMapper.class);
		List<Material> materials=mm.selectMaterial();
		for(Material material:materials){
			System.out.println(material);
		}
	}
	public void addMaterial(Material material){
		MaterialMapper mm=session.getMapper(MaterialMapper.class);
		mm.insert(material);
		session.commit();
	}
	public void changeMaterial(Material material){
		MaterialMapper mm=session.getMapper(MaterialMapper.class);
		mm.updateByPrimaryKeySelective(material);
		session.commit();
	}
	public  void  producePurchaseOrder(PurchaseOrder purchaseOrder){
		PurchaseOrderMapper po=session.getMapper(PurchaseOrderMapper.class);
		po.insert(purchaseOrder);
		session.commit();
	}
	public void  selectPurchaseOrder(){
		PurchaseOrderMapper po=session.getMapper(PurchaseOrderMapper.class);
		List<PurchaseOrder> purchaseOrder=po.selectPurchaseOrder();
		for(PurchaseOrder por:purchaseOrder){
			System.out.println(por);
		}
	}
	public void changePurchaseOrder(PurchaseOrder purchaseOrder){
		PurchaseOrderMapper po=session.getMapper(PurchaseOrderMapper.class);
		po.updateByPrimaryKeySelective(purchaseOrder);
		session.commit();
	}
	public  void  addPurchasePlanning(PurchasePlanning purchasePlanning){
		PurchasePlanningMapper ppm=session.getMapper(PurchasePlanningMapper.class);
		ppm.insert(purchasePlanning);
		session.commit();
	}
	public void  selectPurchasePlanning(){
		PurchasePlanningMapper ppm=session.getMapper(PurchasePlanningMapper.class);
		List<PurchasePlanning> purchasePlanning=ppm.selectPurchasePlanning();
		for(PurchasePlanning pp:purchasePlanning){
			System.out.println(pp);
		}
	}
	public void getPurchasePlanningByStatus(){
		PurchasePlanningMapper ppm=session.getMapper(PurchasePlanningMapper.class);
		List<PurchasePlanning> purchasePlanning=ppm.getPurchasePlanningByStatus();
		for(PurchasePlanning pp:purchasePlanning){
			System.out.println(pp);
		}
	}
	public void changePurchasePlanning(PurchasePlanning purchasePlanning){
		PurchasePlanningMapper ppm=session.getMapper(PurchasePlanningMapper.class);
		ppm.updateByPrimaryKeySelective(purchasePlanning);
		session.commit();
	}
	
	public void produceStorageRecord(StorageRecord storageRecord){
		StorageRecordMapper stm =session.getMapper(StorageRecordMapper.class);
		stm.insert(storageRecord);
		session.commit();
	}
	public void selectStorageRecord(){
		StorageRecordMapper stm =session.getMapper(StorageRecordMapper.class);
		List<StorageRecord> storageRecord=stm.selectStorageRecord();
		for(StorageRecord st:storageRecord){
			System.out.println(st);
		}
	}
	public void produceStorageML(StorageML storageML){
		StorageMLMapper sml=session.getMapper(StorageMLMapper.class);
		sml.insert(storageML);
		session.commit();
	}
	public void selectStorageML(){
		StorageMLMapper sml=session.getMapper(StorageMLMapper.class);
		List<StorageML> storageML=sml.selectStorageML();
		for(StorageML sl:storageML){
			System.out.println(sl);
		}
	}
	public void addCollarSingle(CollarSingle collarSingle){
		CollarSingleMapper csm=session.getMapper(CollarSingleMapper.class);
		csm.insert(collarSingle);
		session.commit();
	}
	public void changeCollarSingle(CollarSingle collarSingle){
		CollarSingleMapper csm=session.getMapper(CollarSingleMapper.class);
		csm.updateByPrimaryKeySelective(collarSingle);
		session.commit();
	}
	public void selectCollarSingle(){
		CollarSingleMapper csm=session.getMapper(CollarSingleMapper.class);
		List<CollarSingle> collarSingle=csm.selectCollarSingle();
		for(CollarSingle cs:collarSingle){
			System.out.println(cs);
		}
	}
	
	public void produceLibraryRecord(LibraryRecord libraryRecord){
		LibraryRecordMapper lrm=session.getMapper(LibraryRecordMapper.class);
		lrm.insert(libraryRecord);
		session.commit();
	}
	public void selectLibraryRecord(){
		LibraryRecordMapper lrm=session.getMapper(LibraryRecordMapper.class);
		List<LibraryRecord> libraryRecord=lrm.selectLibraryRecord();
		for(LibraryRecord lr:libraryRecord){
			System.out.println(lr);
		}
	}
	public void addDocumentRecord(DocumentRecord documentRecord){
		DocumentRecordMapper drm=session.getMapper(DocumentRecordMapper.class);
		drm.insert(documentRecord);
		session.commit();
	}
	public void selectDocumentRecord(){
		DocumentRecordMapper drm=session.getMapper(DocumentRecordMapper.class);
		List<DocumentRecord> documentRecord=drm.selectDocumentRecord();
		for(DocumentRecord  dr:documentRecord){
			System.out.println(dr);
		}
	}
	public void producePurchasePlanML(PurchasePlanML purchasePlanML){
		PurchasePlanMLMapper ppm=session.getMapper(PurchasePlanMLMapper.class);
		ppm.insert(purchasePlanML);
		session.commit();
	}
	public void selectPurchasePlanML(){
		PurchasePlanMLMapper ppm=session.getMapper(PurchasePlanMLMapper.class);
		List<PurchasePlanML> purchasePlanML=ppm.selectPurchasePlanML();
		for(PurchasePlanML pp:purchasePlanML){
			System.out.println(pp);
		}
	}
	public void produceCollarSingleML(CollarSingleML collarSingleML){
		CollarSingleMLMapper csml=session.getMapper(CollarSingleMLMapper.class);
		csml.insert(collarSingleML);
		session.commit();
	}
	public void selectCollarSingleML(){
		CollarSingleMLMapper csml=session.getMapper(CollarSingleMLMapper.class);
		List<CollarSingleML> collarSingleML=csml.selectCollarSingleML();
		for(CollarSingleML cs:collarSingleML){
			System.out.println(cs);
		}
	}
	public void produceLibraryRecordML(LibraryRecordML libraryRecordML){
		LibraryRecordMLMapper lrm=session.getMapper(LibraryRecordMLMapper.class);
		lrm.insert(libraryRecordML);
		session.commit();
	}
	public void selectLibraryRecordML(){
		LibraryRecordMLMapper lrm=session.getMapper(LibraryRecordMLMapper.class);
		List<LibraryRecordML> libraryRecordML=lrm.selectLibraryRecordML();
		for(LibraryRecordML lr:libraryRecordML){
			System.out.println(lr);
		}
	}
	public void producePurchaseOrderML(PurchaseOrderML purchaseOrderML){
		PurchaseOrderMLMapper pom=session.getMapper(PurchaseOrderMLMapper.class);
		pom.insert(purchaseOrderML);
		session.commit();
	}
	public void selectPurchaseOrderML(){
		PurchaseOrderMLMapper pom=session.getMapper(PurchaseOrderMLMapper.class);
		List<PurchaseOrderML> purchaseOrderML=pom.selectPurchaseOrderML();
		for(PurchaseOrderML po:purchaseOrderML){
			System.out.println(po);
		}
	}
	public int getPurchaePlanningId(String code){
		PurchasePlanningMapper ppm =session.getMapper(PurchasePlanningMapper.class);
		PurchasePlanning pp =ppm.getPurchasePlanningByCode(code);
		return pp.getId();	
	}
    public int getPurchaseOrderId(String code){
    	PurchaseOrderMapper pom=session.getMapper(PurchaseOrderMapper.class);
    	PurchaseOrder po =pom.getPurchaseOrderByCode(code);
    	return po.getId();
    }
    public int getStorageRecordIdByCode(String code){
    	StorageRecordMapper stm=session.getMapper(StorageRecordMapper.class);
    	StorageRecord st =stm.getStorageRecordByCode(code);
    	return st.getId();
    }
    public int getCollarSingleIdByCode(String code){
    	CollarSingleMapper csm=session.getMapper(CollarSingleMapper.class);
    	CollarSingle  cs =csm.getCollarSinleByCode(code);
    	return cs.getId();
    }
    public int getLibraryRecordIdByCode(String code){
    	LibraryRecordMapper lrm=session.getMapper(LibraryRecordMapper.class);
    	LibraryRecord lr =lrm.getLibraryRecordByCode(code);
    	return lr.getId();
    }
    public PurchasePlanning getPurchasePlanningById(int id){
    	PurchasePlanningMapper ppm=session.getMapper(PurchasePlanningMapper.class);
    	PurchasePlanning  purchasePlanning =ppm.selectByPrimaryKey(id);
    	return purchasePlanning;
    }
    public PurchaseOrder getPurchaseOrderById(int id){
    	PurchaseOrderMapper pom=session.getMapper(PurchaseOrderMapper.class);
    	PurchaseOrder  purchaseOrder =pom.selectByPrimaryKey(id);
    	return purchaseOrder;
    }
    public PurchasePlanML getByPurchasePlanningId(int id ){
    	PurchasePlanMLMapper pp=session.getMapper(PurchasePlanMLMapper.class);
    	PurchasePlanML ppml=pp.getByPurchasePlanningId(id);
    	return ppml;
    }
    public PurchaseOrderML getByPurchaseOrderId(int id){
    	PurchaseOrderMLMapper poml=session.getMapper(PurchaseOrderMLMapper.class);
    	PurchaseOrderML po=poml.getByPurchaseOrderId(id);
    	return po;
    }
    public CollarSingle getCollarSingleById(int id ){
    	CollarSingleMapper csm=session.getMapper(CollarSingleMapper.class);
    	CollarSingle cs=csm.selectByPrimaryKey(id);
    	return cs;
    }
    public CollarSingleML getByCollarSingleId(int id){
    	CollarSingleMLMapper csml=session.getMapper(CollarSingleMLMapper.class);
    	CollarSingleML cs=csml.getByCollarSingleId(id);
    	return cs;
    	
    }
    public StorageML getByMaterialId(int id ){
    	StorageMLMapper sml=session.getMapper(StorageMLMapper.class);
    	StorageML storageML=sml.getByMaterialId(id);
    	return storageML;
    }
	public boolean isUserNameExists(String name){
		UserMapper um =session.getMapper(UserMapper.class);
		User user =um.getUserByName(name);
		if(user!=null){
			return true;
		}
		return false;
	}
	public boolean isProjectNameExists(String name){
		ProjectMapper pm =session.getMapper(ProjectMapper.class);
		Project project=pm.getProjectByName(name);
		if(project!=null){
			return true;
		}
		return false;
	}
	public boolean isProjectCodeExists(String code){
		ProjectMapper pm =session.getMapper(ProjectMapper.class);
		Project project=pm.getProjectByCode(code);
		if(project!=null){
			return true;
		}
		return false;
	}
	public boolean isMaterialNameExists(String name){
		MaterialMapper mm =session.getMapper(MaterialMapper.class);
		Material material=mm.getMaterialByName(name);
		if(material!=null){
			return true;
		}
		return false;
	}
	public boolean isMaterialCodeExists(String code){
		MaterialMapper mm =session.getMapper(MaterialMapper.class);
		Material material=mm.getMaterialByCode(code);
		if(material!=null){
			return true;
		}
		return false;
	}
	public boolean isPurchasePlanCodeExists(String code){
		PurchasePlanningMapper ppm =session.getMapper(PurchasePlanningMapper.class);
		PurchasePlanning  pp=ppm.getPurchasePlanningByCode(code);
		if(pp!=null){
			return true;
		}
		return false;
	}
	public boolean isPurchaseOrderCodeExists(String code){
		PurchaseOrderMapper pom =session.getMapper(PurchaseOrderMapper.class);
		PurchaseOrder po=pom.getPurchaseOrderByCode(code);
		if(po!=null){
			return true;
		}
		return false;
	}
	public boolean isStorageRecordCodeExists(String code){
		StorageRecordMapper srm=session.getMapper(StorageRecordMapper.class);
		StorageRecord sr =srm.getStorageRecordByCode(code);
		if(sr!=null){
			return true;
		}
		return false;
	}
	public boolean isPurchasePlanningIdExists(int id){
		PurchasePlanningMapper ppm=session.getMapper(PurchasePlanningMapper.class);
		PurchasePlanning pp=ppm.selectByPrimaryKey(id);
		if(pp!=null){
			return true;
		}
		return false;
	}
	public boolean isPurchaseOrderIdExists(int id){
		PurchaseOrderMapper pom=session.getMapper(PurchaseOrderMapper.class);
		PurchaseOrder po=pom.selectByPrimaryKey(id);
		if(po!=null){
			return true;
		}
		return false;
	}
	public boolean isCollarSingleIdExists(int id){
		CollarSingleMapper csm=session.getMapper(CollarSingleMapper.class);
		CollarSingle cs=csm.selectByPrimaryKey(id);
		if(cs!=null){
			return true;
		}
		return false;
	}
	public boolean isProjectIdExists(int id){
		ProjectMapper pm=session.getMapper(ProjectMapper.class);
		Project project =pm.selectByPrimaryKey(id);
		if(project!=null){
			return true;
		}
		return false;
	}
	public boolean isMaterialIdExists(int id){
		MaterialMapper mm=session.getMapper(MaterialMapper.class);
		Material material =mm.selectByPrimaryKey(id);
		if(material!=null){
			return true;
		}
		return false;
	}
	public boolean isCollarSingleCodeExists(String code) {
		CollarSingleMapper csm=session.getMapper(CollarSingleMapper.class);
		CollarSingle cs =csm.getCollarSinleByCode(code);
		if(cs!=null){
			return true;
		}
		return false;
	}
	public boolean isStorageMLMaterialExists(int id ){
		StorageMLMapper stml =session.getMapper(StorageMLMapper.class);
		StorageML sml=stml.getByMaterialId(id);
		if(sml!=null){
			return true;
		}
		return false;
	}
	public boolean isLibraryRecordCodeExists(String code){
		LibraryRecordMapper lrm=session.getMapper(LibraryRecordMapper.class);
		LibraryRecord lr =lrm.getLibraryRecordByCode(code);
		if(lr!=null){
			return true;
		}
		return false;
	}
	public boolean isDocumentRecordCodeExists(String code){
		DocumentRecordMapper drm=session.getMapper(DocumentRecordMapper.class);
		DocumentRecord gr =drm.getDocumentRecordByCode(code);
		if(gr!=null){
			return true;
		}
		return false;
	}
	public boolean isUserIdExists(int id){
		UserMapper um=session.getMapper(UserMapper.class);
		User user=um.selectByPrimaryKey(id);
		if(user!=null){
			return true;
		}
		return false;
	}
	
	
	
	

}
